<template>
  <div class="detail_good_container">
    <div class="top">
      <DetailTop />
    </div>
    <div class="content">
      <div class="left">
        <div class="pic_list">
          <div class="item" :class="{ isActive: activeIndex === index }"
            v-for="(item, index) in goodDetailStore.goodDetail?.images" :key="index" @click="setActive(index)">
            <img :src="item" alt="">
          </div>
        </div>
        <div class="pic_detail">
          <img v-if="goodDetailStore.goodDetail?.images?.[activeIndex]"
            :src="goodDetailStore.goodDetail?.images?.[activeIndex]" :key="activeIndex" alt="">
        </div>

      </div>
      <div class="right">
        <div class="de_info">
          <span class="price">¥{{ goodDetailStore.goodDetail?.price }}</span>
          <span class="category">{{ goodDetailStore.goodDetail?.category }}</span>
        </div>
        <div class="info">
          {{ goodDetailStore.goodDetail?.description }}
        </div>
        <div class="btn">
          <span>
            <button class="contact" @click="goMessage">联系卖家</button>
          </span>
          <span>
            <button class="buying" @click="goBuy">立即购买</button>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import DetailTop from './DetailTop.vue';
import { useGoodDetailStore } from '@/stores/module/goods/good';
import { useMessageStore } from '@/stores/module/message';
import { useOrderStore } from '@/stores/module/order/order';
import { useChatsStore } from '@/stores/module/chats/chats';
import { getSellerChat } from '@/service/module/message/message';
import { message } from 'ant-design-vue';
import { useRouter } from 'vue-router';

import { useRoute } from 'vue-router';
const route = useRoute();
const goodDetailStore = useGoodDetailStore()
const queryId = route.params.id as string;

const messageStore = useMessageStore()
const orderStore = useOrderStore()
const chatsStore = useChatsStore()
const router = useRouter()
const activeIndex = ref(0)
function setActive(index: number) {
  activeIndex.value = index
}
const sellerData = ref()

async function getSellerId() {
  await goodDetailStore.getGoodDetail(queryId);
  sellerData.value = await getSellerChat(goodDetailStore.goodDetail.seller.id)
}
getSellerId()
const goMessage = async () => {
  if (goodDetailStore.goodDetail.putawayNumber == 0) {
    message.warn('该商品已被售出')
    router.back()
    return
  }
  if (sellerData.value.data) {

    chatsStore.goSellerId = sellerData.value.data
    chatsStore.activeKey = "true"
    messageStore.showMessage = true

  } else {
    message.info('这是你自己的商品')
  }
  // chatsStore.currentChat = res.data.list[0].roomId

}
const goBuy = async () => {

  if (goodDetailStore.goodDetail.putawayNumber == 0) {
    message.warn('该商品已被售出')
    router.back()
    return
  }
  if (sellerData.value.data) {
    orderStore.isShow = true
    orderStore.id = goodDetailStore.goodDetail?.id
  } else {
    message.info('这是你自己的商品')
  }

}
</script>

<style lang="less" scoped>
@import "../../../assets/css/primary/index.less";



.detail_good_container {
  width: 1296px;
  height: 94%;
  padding: 12px;
  display: flex;
  flex-direction: column;

  .top {
    width: 100%;
  }

  .content {
    margin-top: 10px;
    width: 100%;
    height: 100%;
    background-color: #fff;
    border-radius: 15px;
    display: flex;

    .left {
      flex: 3;
      width: 100%;
      height: 500px;
      display: flex;

      .pic_list {
        padding: 8px;
        width: 100px;
        display: flex;
        flex-direction: column;
        align-items: center;
        overflow-y: auto;

        &::-webkit-scrollbar {
          display: none;
        }

        .item {
          box-sizing: border-box;
          margin-top: 8px;
          width: 90px;
          height: 90px;
          background-color: #fff;
          border-radius: 10px;
          margin-bottom: 8px;
          display: flex;
          justify-content: center;
          border: 1px solid transparent;

          img {
            height: 88px;
            width: 100%;
            object-fit: cover; // 保持比例，超出裁剪
            object-position: center; // 居中显示
            border-radius: 10px;
            display: block;
          }
        }

        .isActive {
          border: 1px solid #ccc;
        }
      }

      .pic_detail {
        padding: 12px;
        width: 100%;
        display: flex;
        justify-content: center;

        img {
          border-radius: 12px;
          width: 100%;
          height: 480px;
          object-fit: cover; // 保持比例，超出裁剪
          object-position: center; // 居中显示
          border-radius: 10px;
          display: block;
        }
      }
    }

    .right {
      flex: 2;
      display: flex;
      flex-direction: column;
      padding: 25px;

      .de_info {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;

        .price {
          font-size: 24px;
          font-weight: 600;
          color: rgb(238, 36, 36);
        }

        .category {
          font-size: 14px;
          color: #6d6a6a;
        }
      }


      .info {
        max-height: 400px;
        margin-top: 18px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 12;
        -webkit-box-orient: vertical;
        text-overflow: ellipsis;
        word-break: break-all;
      }

      .btn {
        margin-top: 18px;
        display: flex;
        height: 66px;

        span {
          width: 130px;
          height: 50px;

          button {
            width: 130px;
            height: 50px;
            outline: none;
            border: none;
            cursor: pointer;
          }

          .contact {
            border-top-left-radius: 24px;
            border-bottom-left-radius: 24px;
            background-color: @primary-color;
          }

          .buying {
            border-top-right-radius: 24px;
            border-bottom-right-radius: 24px;
            background-color: rgb(197, 190, 179);
          }
        }
      }
    }
  }
}


@media (max-width: 1296px) {
  .detail_good_container {
    width: 1040px;
  }

}
</style>
